package com.magic233.controller;

import com.magic233.entity.Orders;
import com.magic233.entity.Users;
import com.magic233.service.LogsService;
import com.magic233.service.OrdersService;
import com.magic233.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * ClassName: ManagerController
 * Description:
 * date: 2021/6/3 15:59
 * author: zouyuan
 */
@RestController
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    private LogsService logsService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private OrdersService ordersService;

    @RequestMapping("/selectUsersByDept")
    public Object selectUsersByDept(HttpServletRequest request) {
        Users user = (Users) request.getAttribute("user");
        String dept = user.getDept();
        System.out.println(dept + "部门负责人查询部门成员");
        List<Users> users = usersService.selectUsersByDept(dept);
        return users;
    }


    @RequestMapping("/addDeptUser")
    public Object addDeptUser(HttpServletRequest request, Long id, String phone, String username) {
        Users manager = (Users) request.getAttribute("user");
        String dept = manager.getDept();
        Users user = new Users();
        user.setId(id);
        user.setPhone(phone);
        user.setUsername(username);
        user.setPassword(phone);
        user.setDept(dept);
        user.setDeleted(0);
        int re = usersService.addUser(user);
        if (re == 1) {
            String thing = "部门负责人：" + "{id:" + manager.getId() + ",username:" + manager.getUsername() + ",phone:" + manager.getPhone() + "}" + "添加了部门成员：" + "{id:" + id + ",username:" + username + ",phone:" + phone + "}";
            logsService.addLog(thing);
        }
        return re;
    }

    @RequestMapping("/deleteDeptUser")
    public Object deleteDeptUser(HttpServletRequest request, Long id) {
        System.out.println("部门负责人删除部门成员");
        if (id == null) {
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int re = usersService.deleteDeptUser(user, id);
        return re;
    }

    //查询工单
    @RequestMapping("/selectDeptOrders")
    public Object selectDeptOrders(HttpServletRequest request, String status, String type, Long startTime, Long endTime, Integer pageNum, Integer pageSize) {
        Users user = (Users) request.getAttribute("user");
        Orders order = new Orders();
        if (status == null || "".equals(status)) {
            order.setStatus("全部状态");
            System.out.println("设置全部状态");
        } else {
            order.setStatus(status);
            System.out.println("设置" + status);
        }
        if (type == null || "".equals(type)) {
            order.setType("全部类别");
            System.out.println("设置全部类别");
        } else {
            order.setType(type);
            System.out.println("设置" + type);
        }
        order.setSelectStartTime(startTime);
        order.setSelectEndTime(endTime);
        Map<Object, Object> map = ordersService.selectDeptOrders(user, order, pageNum, pageSize);
        return map;
    }
}
